package com.woniuxy.dr_mall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.dr_mall.entity.DrManager;
import com.woniuxy.dr_mall.entity.DrMenu;
import com.woniuxy.dr_mall.entity.DrPerm;
import com.woniuxy.dr_mall.exception.AccountNotExistException;
import com.woniuxy.dr_mall.exception.PasswordErrorException;
import com.woniuxy.dr_mall.global.Global;
import com.woniuxy.dr_mall.mapper.*;
import com.woniuxy.dr_mall.service.IDrManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@Service
public class DrManagerServiceImpl extends ServiceImpl<DrManagerMapper, DrManager> implements IDrManagerService {
    @Autowired
    private DrManagerMapper drManagerMapper;
    @Autowired
    private DrPermMapper drPermMapper;
    @Autowired
    private DrMenuMapper drMenuMapper;
    @Autowired
    private DrManagerMenuMapper drManagerMenuMapper;
    @Autowired
    private DrManagerPermMapper drManagerPermMapper;
    @Override
    public DrManager add(DrManager drManager) {
        drManager.setStatus(Global.NORMAL);
     drManagerMapper.add(drManager);
        for (DrMenu menu : drManager.getMenus()) {
            drManagerMenuMapper.add(drManager.getId(),menu.getId());
        }
        for (DrPerm perm : drManager.getPerms()) {
           drManagerPermMapper.add(drManager.getId(),perm.getId());
        }
        return drManagerMapper.getById(drManager.getId());
    }

    @Override
    public DrManager getByAccount(String account, String password) {
        DrManager drManager = drManagerMapper.getByAccount(account);
        if (drManager != null) {
            if (drManager.getPassword().equals(password)) {
                log.debug("登录成功");
                return drManager;
            } else {
                throw new PasswordErrorException("密码错误");
            }
        } else {
            throw new AccountNotExistException("账号不存在");
        }
    }

    @Override
    public List<DrPerm> getPerms() {
        return drPermMapper.getPerms();
    }

    @Override
    public void updateNormalStatus(int id) {
        drManagerMapper.getNormal(id);
    }

    @Override
    public void updateUnNormalStatus(int id) {
        drManagerMapper.getUnNormal(id);
    }

    @Override
    public List<DrMenu> getMenus() {
        return drMenuMapper.getMenus();
    }

    @Override
    public List<DrManager> getDrManagers() {
        return drManagerMapper.getDrManagers();
    }

    @Override
    public DrManager getDrManagerById(int id) {
        return drManagerMapper.getById(id);
    }

}
